package com.order.controller.buyer;

import com.baiyexing.enums.ResultEnum;
import com.baiyexing.exception.SellException;
import com.baomidou.mybatisplus.extension.api.R;
import com.order.annotation.LoggerManage;
import com.order.converter.OrderForm2OrderDTOConverter;
import com.order.dto.OrderDTO;
import com.order.dto.OrderDetailsDTO;
import com.order.from.OrderForm;
import com.order.mapper.OrderMasterMapper;
import com.order.pojo.ProductCategory;
import com.order.service.IOrderMasterService;
import com.order.service.PushMessageService;
import com.order.utils.Result;
import com.order.vo.ResultVo;
import com.order.websocket.WebSocket;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "买家订单接口")
@RestController
@ApiResponses({
        @ApiResponse(code = 200, message = "操作成功", response = ProductCategory.class),
        @ApiResponse(code = 400, message = "参数错误"),
        @ApiResponse(code = 403, message = "权限被禁止"),
        @ApiResponse(code = 500, message = "服务器内部错误")
})
@RequestMapping("/sell/buyer")
@Slf4j
public class BuyerOrderController {

    @Autowired
    IOrderMasterService orderMasterService;
    @Autowired
    PushMessageService pushMessageService;
    @Autowired
    WebSocket webSocket;
    /**
     * /sell/buyer/order/create  买家下单接口
     * 买家下单：OrderForm(name=小李, phone=133345678900, address=小李的地址测试102号, openid=,
     * items=[{"productId":"1955101817658417151","productQuantity":1},{"productId":"1955101817658417152","productQuantity":2}])
     *
     * @return
     */
    @LoggerManage(value = "买家下单")
    @ApiOperation(value = "买家下单", notes = "买家下单....")
    @PostMapping("/order/create")
    public ResultVo createOrder(OrderForm orderForm) {
        OrderDTO orderDTO = OrderForm2OrderDTOConverter.convert(orderForm);
        log.info("买家下单的数据：" + orderDTO);
        //调用下单业务
        OrderDTO order = orderMasterService.createOrder(orderDTO);
        //发送模板消息
        pushMessageService.orderStatus(orderDTO);
        //给卖家推动新的订单消息
        webSocket.sendMessage(order.getOrderId());
        return new ResultVo(0, "成功", order);
    }

    @GetMapping("/order/list")
    public ResultVo<List<OrderDTO>> getlist(@RequestParam("openid") String openid) {
        if (!StringUtils.hasText(openid)) {
            log.error("【查询订单列表】openid为空");
          throw new SellException(ResultEnum.CART_EMPTY);
        }
        List<OrderDTO> orderDTOList = orderMasterService.listbyOd(openid);
        return new ResultVo<>(0, "查询成功", orderDTOList);
    }

    @GetMapping("/order/detail")
    public ResultVo<OrderDetailsDTO> detail(@RequestParam("openid") String openid,
                           @RequestParam("orderId") String orderId) {
        if (!StringUtils.hasText(openid)) {
            log.error("【查询订单列表】openid为空");
            throw new SellException(ResultEnum.CART_EMPTY);
        }
            OrderDetailsDTO orderDetailsDTO = orderMasterService.listbyOdAndid(openid,orderId);
        return new ResultVo(0,"查询成功",orderDetailsDTO);
    }
    @LoggerManage(value = "订单取消")
    @PostMapping("/order/cancel")
    public  ResultVo cancel(@RequestParam("openid") String openid,
                                       @RequestParam("orderId") String orderId){
        orderMasterService.deletebyOdAndid(openid,orderId);
        return new ResultVo<>(0,"取消成功",null);

    }
}
